package lfp.addressselector.svgparser;

import android.content.Context;
import android.graphics.RectF;
import android.os.AsyncTask;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilderFactory;

import lfp.addressselector.bean.PathData;
import lfp.addressselector.util.LogUtil;

/**
 * Created by Administrator on 2017/6/6.
 */

public class SVGReader {

    Context mContext;
    RedderTask mRedderTask;
    IReaderFinish mIReaderFinish;

    public SVGReader(Context c) {
        mContext = c;
    }

    public void setIReaderFinish(IReaderFinish mIReaderFinish) {
        this.mIReaderFinish = mIReaderFinish;
    }

    //读取地图SVG文件
    public void reader(int resId) {
        if (mRedderTask != null) recycle();
        mRedderTask = new RedderTask();
        mRedderTask.execute(resId);
    }

    public boolean isCancelled() {
        return mRedderTask == null ? true : mRedderTask.isCancelled();
    }

    public void recycle() {
        if (mRedderTask != null) {
            if (!isCancelled()) mRedderTask.cancel(true);
            mRedderTask = null;
        }
    }


    private class RedderTask extends AsyncTask<Integer, Void, Object[]> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Object[] doInBackground(Integer... integers) {
            try {
                InputStream svg = mContext.getResources().openRawResource(integers[0]);
                Element Root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(svg).getDocumentElement();
                NodeList node = Root.getElementsByTagName("path");
                MapBouns mMapBouns = new MapBouns();
                ArrayList<PathData> mPathDatas = new ArrayList<>();
                for (int i = 0; i < node.getLength(); i++) {
                    Element pathElement = (Element) node.item(i);
                    PathData mPathData = new PathData();
                    mPathData.setName(pathElement.getAttribute("name"));
                    mPathData.setPath(PathParser.createPathFromPathData(pathElement.getAttribute("android:pathData")));
                    mMapBouns.addPathBouns(mPathData.getBouns());
                    mPathDatas.add(mPathData);
                }
                return new Object[]{mPathDatas, mMapBouns};
            } catch (Exception e) {
                LogUtil.getLogger().e(e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object[] data) {
            super.onPostExecute(data);
            if (isCancelled()) return;
            if (data == null) {
                return;
            }
            ArrayList<PathData> mPathDatas = (ArrayList<PathData>) data[0];
            MapBouns mMapBouns = (MapBouns) data[1];
            if (mIReaderFinish != null) mIReaderFinish.onReaderFinish(mPathDatas, mMapBouns);
        }
    }

    /**
     * 地图范围
     */
    public static final class MapBouns {
        RectF mBouns;

        public MapBouns() {
            mBouns = new RectF(-1, -1, -1, -1);
        }

        public void addPathBouns(RectF bouns) {
            if (mBouns.left == -1 || mBouns.left > bouns.left) mBouns.left = bouns.left;
            if (mBouns.top == -1 || mBouns.top > bouns.top) mBouns.top = bouns.top;
            if (mBouns.right == -1 || mBouns.right < bouns.right) mBouns.right = bouns.right;
            if (mBouns.bottom == -1 || mBouns.bottom < bouns.bottom) mBouns.bottom = bouns.bottom;
        }

        public float getWidth() {
            return mBouns.right - mBouns.left;
        }

        public float getHeight() {
            return mBouns.bottom - mBouns.top;
        }


    }


    public interface IReaderFinish {

        /**
         * 地图读取完成后的回调
         *
         * @param mPathDatas 地图板块路径集合
         * @param mMapBouns  整个地图范围
         */
        void onReaderFinish(ArrayList<PathData> mPathDatas, MapBouns mMapBouns);
    }


}
